package com.uaroadshow.webpay.processor.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.wechat.entity.pay.PayTransaction;
import com.uaroadshow.webpay.entity.PaymentContext;
import com.uaroadshow.webpay.processor.BusinessProcessor;
import com.uaroadshow.webpay.rao.BusinessRao;

/**
 * 
 * @author mohanlan
 *
 */
@Component("weapppayBusinessProcessor")
public class WeappBusinessProcessorImpl implements BusinessProcessor {
	
	private static final Logger logger = LoggerFactory.getLogger(WeappBusinessProcessorImpl.class);
	
	@Value("#{payment['weapp.business.notifyUrl']}")
    private String notifyUrl;
	
	@Rao
    private BusinessRao businessRao;
	
	/*
	 * (non-Javadoc)
	 * @see com.uaroadshow.webpay.processor.BusinessProcessor#notice(com.uaroadshow.webpay.entity.PaymentContext, com.demo2do.core.wechat.entity.pay.PayTransaction)
	 */
    public void notice(PaymentContext paymentContext, PayTransaction payTransaction) {
        
        // We do not care about the result of business application synchronization.
        // Therefore, we just post data back but ignore the response.
            
    	Map<String, String> notifyParams = new HashMap<String, String>();
    	
    	notifyParams.put("requestNo", paymentContext.getToken());
    	notifyParams.put("orderNo", paymentContext.getOrderCode());
    	notifyParams.put("transactionCode", payTransaction.getTransactionCode());
    	notifyParams.put("amount", String.valueOf(payTransaction.getAmount()));
    	notifyParams.put("paymentTime", payTransaction.getPaymentTimeAsString());

    	
        logger.trace("#notice - start call notifyUrl[{}], notifyParams[{}]", notifyUrl, JsonUtils.toJson(notifyParams));
        
        HttpResult httpResult = businessRao.notify(notifyUrl, notifyParams);
        
        if(!httpResult.isValid()) {
        	logger.error("#notice - notice business occurs error, the result is [{}]", JsonUtils.toJson(httpResult));
        }
        
    }

}
